import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { AddCollectionInvoiceModalComponent } from 'src/app/routes/ticket-management/components/input-invoice/add-collection-invoice-modal/add-collection-invoice-modal.component';
import { TicketService } from 'src/app/routes/ticket-management/services/ticket.service';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-payment-order',
  templateUrl: './payment-order.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class PaymentOrderComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      let params = { ...this.sf?.value };
      if (params.payDate) {
        const payDate = { start: this.sf?.value?.payDate?.[0], end: this.sf?.value?.payDate?.[1] };
        params.payDate = payDate;
      }
      if (params.payDate2) {
        const payDate2 = { start: this.sf?.value?.payDate2?.[0], end: this.sf?.value?.payDate2?.[1] };
        params.payDate2 = payDate2;
      }
      if (params.createTime) {
        const createTime = { start: this.sf?.value?.createTime?.[0] || '', end: this.sf?.value?.createTime?.[1] || '' };
        params.createTime = createTime;
      }
      Object.assign(requestOptions.body, params);
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择付款单');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '付款单',
      nzContent: AddCollectionInvoiceModalComponent,
      nzComponentParams: { i: { userId: 0 } },
      nzFooter: null
    });
  }

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_payment_page_export);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        paycode: {
          type: 'string',
          title: '付款单号'
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: null
        },
        // orderS2n: {
        //   type: 'string',
        //   title: '付款状态',
        //   ui: {
        //     widget: 'dict-select',
        //     containsAllLable: true,
        //     params: { dictKey: 'refund:apply:status' },
        //     placeholder: '请选择'
        //   }
        // },
        paytype: {
          type: 'string',
          title: '付款类型',
          ui: {
            widget: 'dict-select',
            containsAllLabel: true,
            params: { dictKey: 'pay:type' },
            placeholder: '请选择'
          }
        },
        paymode: {
          type: 'string',
          title: '付款方式',
          ui: {
            widget: 'dict-select',
            containsAllLabel: true,
            params: { dictKey: 'pay:mode' },
            placeholder: '请选择'
          }
        },
        arto: {
          type: 'string',
          title: '收款人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true
          }
        },
        payDate: {
          title: '要求付款日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        payDate2: {
          title: '确认日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        waybillCode: {
          type: 'string',
          title: '运单号'
        },
        // feeCode: {
        //   type: 'string',
        //   title: '费用号',
        //   ui: {
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        payRemarks: {
          type: 'string',
          title: '付款备注',
          ui: {
            placeholder: '请选择'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', fixed: 'left', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '付款单号', index: 'payCode', className: 'text-left', type: 'link', width: 180 },
      { title: '网络货运人', index: 'ltdName', className: 'text-left', width: 180 },
      { title: '运单号', index: 'waybillCode', className: 'text-left', width: 180 },
      // { title: '费用号', index: 'feeCode', width: 180 },
      { title: '要求付款日期', index: 'payDate', type: 'date', className: 'text-center', width: 150 },
      {
        title: '付款金额',
        index: 'payMoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.payMoney }) }
      },
      { title: '付款类型', index: 'payTypeLabel', width: 130 },
      { title: '付款方式', index: 'payBankTypeLabel', width: 130 },
      { title: '结算客户', index: 'cnoName', width: 160 },
      { title: '收款人', index: 'hrToLabel', width: 150 },
      { title: '应付已核销', index: 'ishrhxLabel', width: 150 },
      { title: '确认日期', index: 'payDate2', type: 'date', className: 'text-center', width: 150 },
      { title: '创建时间', index: 'createTime', type: 'date', className: 'text-center', width: 150 },
      { title: '创建人', index: 'createUserIdLabel', width: 160 },
      { title: '付款备注', index: 'payRemarks', width: 200 },
      {
        title: '操作',
        className: 'text-center',
        fixed: 'right',
        width: '110px',
        buttons: [
          {
            text: '浏览',
            acl: { ability: ['FINANCIAL-PAYMENT-ORDER-view'] },
            click: item => this.router.navigate(['/financial-management/payment-order/detail/' + item.id])
          }
          // {
          //   text: '修改',
          // }
        ]
      }
    ];
  }
}
